<?php

/*
*
* BEGIN mainstatspoller.php
*
*
* Dazzler Server Client Communication Interface - Main Stats Poller
* @Description: This subroutine pushes data to the client when new data is avaialable
* @Authors: Salman Noor, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, Salman, 12 October 2011
*
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/

$lastmodif    = isset($_REQUEST['mainStatsTimeStamp']) ? $_REQUEST['mainStatsTimeStamp'] : -1;
$GameID = isset($_REQUEST['GameID']) ? $_REQUEST['GameID'] : null;
$StatsAdmin = new StatsAdmin();
if ($GameID != NULL)
{
  $currentmodif =  $StatsAdmin->getLastBallId($GameID);
}
else
{
       trigger_error("Long Polling parameters are not set properly. Game ID is not set", E_USER_ERROR);
}


while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
  usleep(10000); // sleep 10ms to unload the CPU
  clearstatcache();

    $currentmodif =  $StatsAdmin->getLastBallId($GameID);
}

// return a json array
$response = array();
$battingandBowlingTeamsArray = $StatsAdmin->getBattingAndBowlingTeams($GameID);
$CurrentScoreArray = $StatsAdmin->getCurrentScore($GameID);
$CommentaryMessage = $StatsAdmin->getLastBallCommentary($GameID);
$response['score']       = $CurrentScoreArray;
$response['ballCommentary']       = $CommentaryMessage;
$response['newTimeStamp'] = $currentmodif;
$response['teams'] = $battingandBowlingTeamsArray;
echo json_encode($response);

flush();

?>
